<?php

/**
 * @author  PVer
 * @date    2018-5-7 11:14:17
 * @version V1.0
 * @desc    
 */

namespace app\daikuan\controller;

use cmf\controller\AdminBaseController;
use think\Validate;
use app\user\model\UserModel;
use app\daikuan\model\LogisticsModel;
use app\user\model\UserLevelModel;
use app\daikuan\model\AuthrecordModel;
use app\daikuan\model\DriverrecordModel;
use app\daikuan\model\RoleUserModel;

class DriverController extends AdminBaseController {

    public function index() {
        $map = array();
//        table排序
        $order = array();
        $order_type = input('order') ?? 'asc';
        $order_name = input('order_name') ?? 'a.can_loan_amount';
        if (empty($order_name)) {
            $order_name = 'a.can_loan_amount';
        }
        $order = "$order_name $order_type";

        $name = input('name');
        if (!empty($name)) {
            $map['a.user_nickname|a.mobile|a.id'] = ['like', '%' . $name . '%'];
        }
        $map['a.user_category'] = 1;
        $dk_status = input('dk_status', -1, 'intval');
        if ($dk_status >= 0) {
            $map['a.dk_status'] = $dk_status;
        }
        $userModel = new UserModel();
        $list = db('user a ')->join(array(array('dk_driverrecord b', 'a.id=b.UserId', 'LEFT')
                    , array('dk_user c', 'a.logistics=c.id', 'LEFT')
                ))
                ->field(' a.user_nickname,a.avatar,a.birthday,a.create_time,a.id,a.logistics,a.mobile,a.number,a.dk_status,(select count(*) from dk_driver_loan where DriverId=a.id and Status in (3,4)) isDaikuan,a.user_level,a.can_loan,a.can_loan_amount,c.user_nickname LogisticsName')
                ->where($map)
                ->order($order)
                ->paginate(10, false, ['query' => request()->param(),]);
        $page = $list->render();
        $this->assign('order', $order_type);
        $this->assign('dk_status', $dk_status);
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function add() {
        $userModel = new UserModel();
        $loglist = $userModel->where("user_category=2")->select();
        $userLevelModel = new UserLevelModel();
        $levellist = $userLevelModel->select();
        $this->assign('levellist', $levellist);
        $this->assign('log_list', $loglist);
        return $this->fetch();
    }

    public function modifypass() {
        $id = input('id');
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function modifypassPost() {

        if ($this->request->isPost()) {
            $data = $this->request->post();

            $userModel = new UserModel();
            $user = array();
            $user['user_pass'] = cmf_password($data['user_pass']);
            $result = $userModel->save($user, 'id=' . $data['id']);
            if ($result !== false) {
                $this->success("操作成功", url("index"));
            } else {
                $this->error("操作失败");
            }
        } else {
            $this->error("数据错误");
            return $this->fetch('index');
        }
    }

    public function edit() {
        $id = input('id');
        $map = array();
        $map['a.user_category'] = 1;
        $map['a.id'] = $id;
        $userModel = new UserModel();
        $loglist = $userModel->where("user_category=2")->select();
        $userLevelModel = new UserLevelModel();
        $levellist = $userLevelModel->select();
        $condition = '';
        $info = db('user a ')->join(array(array('dk_driverrecord b', 'a.id=b.UserId', 'LEFT')
                    , array('dk_user c', 'a.logistics=c.id', 'LEFT')
                    , array('dk_authrecord d', 'a.id=d.UserId', 'LEFT')
                    , array('dk_driverrecord e', 'a.id=e.UserId', 'LEFT')
                ))
                ->field(' a.user_nickname,a.avatar,a.birthday,a.create_time,a.id,a.logistics,a.mobile,a.number,a.dk_status,(select count(*) from dk_driver_loan where DriverId=a.id and Status in (3,4)) isDaikuan,a.user_level,a.can_loan,a.can_loan_amount,d.Img1,d.Img2,d.Img3,d.Img4,e.Img1 Img5,e.Img2 Img6,e.Img3 Img7,e.Img4 Img8,c.user_nickname LogisticsName')
                ->where($condition)
                ->where($map)
                ->find();

        $this->assign('levellist', $levellist);
        $this->assign('log_list', $loglist);
        $this->assign('info', $info);
        if (empty($info)) {
            $this->error("数据错误");
        }
        return $this->fetch();
    }

    public function audit() {
        $id = input('id');
        $map = array();
        $map['a.user_category'] = 1;
        $map['a.id'] = $id;
        $userModel = new UserModel();

        $info = db('user a ')->join(array(array('dk_driverrecord b', 'a.id=b.UserId', 'LEFT')
                    , array('dk_user c', 'a.logistics=c.id', 'LEFT')
                    , array('dk_authrecord d', 'a.id=d.UserId', 'LEFT')
                    , array('dk_driverrecord e', 'a.id=e.UserId', 'LEFT')
                ))
                ->field(' a.user_nickname,a.avatar,a.birthday,a.create_time,a.id,a.logistics,a.mobile,a.number,a.dk_status,(select count(*) from dk_driver_loan where DriverId=a.id and Status in (3,4)) isDaikuan,a.user_level,a.can_loan,a.can_loan_amount,d.Img1,d.Img2,d.Img3,d.Img4,e.Img1 Img5,e.Img2 Img6,e.Img3 Img7,e.Img4 Img8,c.user_nickname LogisticsName')
                ->where($condition)
                ->where($map)
                ->find();
        $this->assign('info', $info);
        if (empty($info)) {
            $this->error("数据错误");
        }
        return $this->fetch();
    }

    public function auditPost() {
        if ($this->request->isPost()) {
            $id = input('id');
            $data = input('post.');
            $result = false;
            $userModel = new UserModel();
            if (isset($data['audit'])) {
                $arr = array();
                $arr["dk_status"] = 1;
                if(!$data['can_loan_amount']){
                    $this->error("贷款额度不能为0");
                }
                $arr["can_loan_amount"] = $data['can_loan_amount'];
                $result = $userModel->save($arr, ['id' => $id]);
            } elseif (isset($data['refuse'])) {
                $arr = array();
                $arr["dk_status"] = 2;
                $arr["can_loan_amount"] = $data['can_loan_amount'];
                $result = $userModel->save($arr, ['id' => $id]);
            }
            if ($result !== false) {
                $this->success("操作成功", url("index"));
            } else {
                $this->error("操作失败");
            }
        }
    }

    public function editPost() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
//修改身份证
            $userModel = new UserModel();
            $user = array();
            $user['user_nickname'] = $data['user_nickname'];
            $user['mobile'] = $data['mobile'];
            $user['user_login'] = $data['mobile'];
            $user['number'] = $data['number'];
            $user['logistics'] = $data['logistics'];
            $user['user_level'] = $data['user_level'];
            $user['can_loan'] = $data['can_loan'];
            $user['can_loan_amount'] = $data['can_loan_amount'];


            $result_1 = $userModel->save($user, 'id=' . $data['id']);
//司机认证信息

            $authrecordModel = new AuthrecordModel();
            $arr1 = array();
            $arr1['UserId'] = $data['id'];
            $arr1['Img1'] = $data['imgid1'];
            $arr1['Img2'] = $data['imgid2'];
            $arr1['Img3'] = $data['imgid3'];
            $arr1['Img4'] = $data['imgid4'];
            $res1 = $authrecordModel->where('UserId=' . $data['id'])->select();
            if (count($res1) > 0) {
                $result_2 = $authrecordModel->save($arr1, 'UserId=' . $data['id']);
            } else {
                $result_2 = $authrecordModel->save($arr1);
            }
//驾驶证认证

            $driverrecordModel = new DriverrecordModel();
            $arr = array();
            $arr['UserId'] = $data['id'];
            $arr['Img1'] = $data['imgid5'];
            $arr['Img2'] = $data['imgid6'];
            $arr['Img3'] = $data['imgid7'];
            $arr['Img4'] = $data['imgid8'];

            $res2 = $driverrecordModel->where('UserId=' . $data['id'])->select();
            if (count($res2) > 0) {
                $result_3 = $driverrecordModel->save($arr, 'UserId=' . $data['id']);
            } else {
                $result_3 = $driverrecordModel->save($arr);
            }


            if ($result_1 !== false && $result_2 !== false && $result_3 !== false) {
                $this->success("操作成功", url("index"));
            } else {
                $this->error("操作失败");
            }
        } else {
            $this->error("请求错误");
            return $this->fetch(':index');
        }
    }

    public function addPost() {
        if ($this->request->isPost()) {


            $validate = new Validate([
                'mobile' => 'require',
                'user_pass' => 'require|min:6|max:12',
                'mobile' => 'require',
            ]);
            $validate->message([
                'user_pass.require' => '密码不能为空',
                'user_pass.min' => '密码长度不能小于6',
                'user_pass.max' => '密码长度最大12',
                'mobile.require' => '手机号码不能为空'
            ]);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }


//            $data = $this->request->post();
//修改身份证
            $userModel = new UserModel();
            $user = array();
            $user['user_login'] = $data['mobile'];
            $user['user_pass'] = cmf_password($data['user_pass']);
            $user['user_nickname'] = $data['user_nickname'];
            $user['mobile'] = $data['mobile'];
            $user['number'] = $data['number'];
            $user['logistics'] = $data['logistics'];
            $user['user_level'] = $data['user_level'];
            $user['can_loan'] = $data['can_loan'];
            $user['can_loan_amount'] = $data['can_loan_amount'];
            $user['user_category'] = 1;
            $user['user_type'] = 4;
            $user['last_login_time'] = strtotime('now');
            $user['create_time'] = strtotime('now');
            $isHave = $userModel->checkDriverCode($user);
            if ($isHave == 1) {
                $this->error("用户名已存在");

                exit();
            }
            if ($isHave == 2) {
                $this->error("手机号已经存在");
                exit();
            }
            $result_1 = $userModel->save($user);

//司机认证信息
            if ($result_1 > 0) {
                $roleUser = new RoleUserModel();
                $role = array();
                $role['role_id'] = 4;
                $role['user_id'] = $userModel->id;
                $roleUser->save($role);
                $authrecordModel = new AuthrecordModel();
                $arr1 = array();
                $arr1['UserId'] = $userModel->id;
                $arr1['Img1'] = $data['imgid1'];
                $arr1['Img2'] = $data['imgid2'];
                $arr1['Img3'] = $data['imgid3'];
                $arr1['Img4'] = $data['imgid4'];
                $res1 = $authrecordModel->where('UserId=' . $userModel->id)->select();
                if (count($res1) > 0) {
                    $result_2 = $authrecordModel->save($arr1, 'UserId=' . $userModel->id);
                } else {
                    $result_2 = $authrecordModel->save($arr1);
                }
//驾驶证认证

                $driverrecordModel = new DriverrecordModel();
                $arr = array();
                $arr['UserId'] = $userModel->id;
                $arr['Img1'] = $data['imgid5'];
                $arr['Img2'] = $data['imgid6'];
                $arr['Img3'] = $data['imgid7'];
                $arr['Img4'] = $data['imgid8'];

                $res2 = $driverrecordModel->where('UserId=' . $userModel->id)->select();
                if (count($res2) > 0) {
                    $result_3 = $driverrecordModel->save($arr, 'UserId=' . $userModel->id);
                } else {
                    $result_3 = $driverrecordModel->save($arr);
                }


                if ($result_1 !== false && $result_2 !== false && $result_3 !== false) {
                    $this->success("操作成功", url("index"));
                } else {
                    $this->error("操作失败");
                }
            } else {
                $this->error("请求错误");
            }
        } else {
            $this->error("请求错误");
            return $this->fetch(':index');
        }
    }

}
